clc; close all; clear;
addpath('../02_Functions');

%------------------------------------------------------------------------%
% This file creates figure 8(a-b) in the paper 
%------------------------------------------------------------------------%

data = load('../01_Data/es_trades_5minute_EST.mat');

No_trades                                                               = data.trades_per_min_mat;
No_contracts                                                            = data.trade_quantity_per_min_mat;

% choose sample period ---------------------------------------------------------------------------------- %

start_date                      = datenum(1998,1,5);
finish_date                     = datenum(2020,12,29);

beg                             = find_closest(data.caldt_daily,start_date);
fin                             = find_closest(data.caldt_daily,finish_date);

caldt                           = data.caldt_daily(beg:fin);
No_trades                       = No_trades(beg:fin,:); 
Volume                          = No_contracts(beg:fin,:); 

fprintf(1,'********************************\n')
fprintf(1,'Sample: Emini          \n')
fprintf('begin %12s  \n',datestr(caldt(1)))
fprintf('finish %12s  \n',datestr(caldt(end)))
fprintf(1,'********************************\n')

% --------------------------------------------------------------------------------------

ADIndex = 1 : 288;              % 18:10 - 18:00
ONIndex = 1 : 186;              % 18:10 - 9:30
IDIndex = 186 : 266;            % 9:30 - 16:15

% ON ID VOLUME TIME SERIES 
No_trades_ON                = sum(No_trades(:,ONIndex),2);    
No_trades_ID                = sum(No_trades(:,IDIndex),2);  
Volume_ON                   = sum(Volume(:,ONIndex),2);  
Volume_ID                   = sum(Volume(:,IDIndex),2);  


caldt                       = datetime(caldt,'ConvertFrom','datenum');
TT                          = timetable(caldt,Volume_ON,Volume_ID,'VariableNames',{'volumeON','volumeID'});
TT                          = retime(TT,'monthly','mean');
TT.caldt                    = dateshift(TT.caldt,'end','month');
Volume_ON_monthly           = TT.volumeON;
Volume_ID_monthly           = TT.volumeID;
caldt_monthly               = TT.caldt;
caldt_monthly               = datenum(caldt_monthly);

% --------------------------------------------------------------------------------------

% HOURLY AVERAGES 
ON_No_trades                = No_trades(:,ONIndex);
ID_No_trades                = No_trades(:,IDIndex); 
ON_Volume                   = Volume(:,ONIndex);
ID_Volume                   = Volume(:,IDIndex); 

% --------------------------------------------------------------------------------------

window                      = 21;
TT                          = length(ON_No_trades);

%------------------------------------------------------------------------%

%%%%% ALL DAY %%%%%

Volume_per_day = sum(Volume,2);

Volume_per_day_RA = zeros(TT,1);
for i = (window+1) : TT
    Volume_per_day_RA(i,1) = mean(Volume_per_day(i - window: i,1));
end

Volume_normalized = zeros(TT,size(Volume,2));
for i = (window+1) : TT
    Volume_normalized(i,:) = Volume(i,:) ./ (1/(length(ADIndex)) * Volume_per_day_RA(i));
end

Volume_per_day_RA           = Volume_per_day_RA(window+1:end,1);
Volume_normalized           = Volume_normalized(window+1:end,:);
TTT                         = length(Volume_normalized);

mu_Volume_normalized        = mean(Volume_normalized,1);
var_Volume_normalized       = var(Volume_normalized,1,1);
se_Volume_normalized        = sqrt(var_Volume_normalized/TTT);


hour_index = 0 : 1 : 288;
x_ticks = 0 : 12 : 288;
hour_string = {'18' , '19' , '20' , '21' , '22' , '23' , '24' , '01' , '02' , '03', '04' , '05' , '06' , '07' , '08' , '9' , '10' , '11' , '12' , '13' , '14' , '15' , '16' , '17' , '18'};

mu_Volume_normalized_plot = [nan mu_Volume_normalized];
se_Volume_normalized_plot = [nan se_Volume_normalized];

figure;
[l, p] = boundedline(hour_index , mu_Volume_normalized_plot , 22*se_Volume_normalized_plot , '-k','alpha');
l.LineWidth = 2;
hold on
plot([25 25], [0 8],'--b','Linewidth',2);
hold on
text(13,8.3,'TSE Open','Color','blue','FontSize',20)
hold on
plot([43 43], [0 7],'--b','Linewidth',2);
hold on
text(27,7.3,'HKE Open','Color','blue','FontSize',20)
hold on
plot([109 109], [0 8],'--b','Linewidth',2);
hold on
text(100,8.8,'LSE Open','Color','blue','FontSize',20)
hold on
text(100,8.3,'FSE Open','Color','blue','FontSize',20)
hold on
plot([187 187], [0 9],'--b','Linewidth',2);
hold on
text(170,9.3,'NYSE Open','Color','blue','FontSize',20)
hold on
plot([268 268], [0 9],'--b','Linewidth',2);
hold on
text(240,9.3,'NYSE Close','Color','blue','FontSize',20)
hold on
plot([hour_index(1) hour_index(end)], [1 1],'-k','Linewidth',1);
hold on
xlim([0 288])
ylabel('Normalized Average Volume Traded' )
xlabel('Trade Time (ET)' )
% ylabel('return (% pa)' )
set(gca,'XTick',x_ticks,'XTickLabel',hour_string);
legend off





%------------------------------------------------------------------------%

%------------------------------------------------------------------------%

asia_start = 1;
asia_end   = 96;
EU_start   = 97;
EU_end     = 156;
OD_start   = 97;
OD_end     = 108;
CTC_start  = 1;
CTC_end    = 267;

ONIndex    = asia_start:EU_end;
Volume     = Volume(:,ONIndex);

%%%%% OVERNIGHT %%%%%

Volume_per_day = sum(Volume,2);

Volume_per_day_RA = zeros(TT,1);
for i = (window+1) : TT
    Volume_per_day_RA(i,1) = mean(Volume_per_day(i - window: i,1));
end

Volume_normalized = zeros(TT,size(Volume,2));
for i = (window+1) : TT
    Volume_normalized(i,:) = Volume(i,:) ./ (1/(length(ONIndex)) * Volume_per_day_RA(i));
end

Volume_per_day_RA           = Volume_per_day_RA(window+1:end,1);
Volume_normalized           = Volume_normalized(window+1:end,:);
TTT                         = length(Volume_normalized);

mu_Volume_normalized        = mean(Volume_normalized,1);
var_Volume_normalized       = var(Volume_normalized,1,1);
se_Volume_normalized        = sqrt(var_Volume_normalized/TTT);


hour_index                  = 0 : 1  : EU_end;
x_ticks                     = 0 : 12 : EU_end;
hour_string                 = {'18' , '19' , '20' , '21' , '22' , '23' , '24' , '01' , '02' , '03', '04', '05', '06', '07'};

mu_Volume_normalized_plot   = [nan mu_Volume_normalized];
se_Volume_normalized_plot   = [nan se_Volume_normalized];

figure;
[l, p] = boundedline(hour_index , mu_Volume_normalized_plot , 22*se_Volume_normalized_plot , '-k','alpha');
l.LineWidth = 2;
hold on
plot([25 25], [0 8],'--b','Linewidth',2);
hold on
text(13,8.3,'TSE Open','Color','blue','FontSize',20)
hold on
plot([43 43], [0 7],'--b','Linewidth',2);
hold on
text(27,7.3,'HKE Open','Color','blue','FontSize',20)
hold on
plot([109 109], [0 8],'--b','Linewidth',2);
hold on
text(100,8.8,'LSE Open','Color','blue','FontSize',20)
hold on
text(100,8.3,'FSE Open','Color','blue','FontSize',20)
hold on
plot([187 187], [0 9],'--b','Linewidth',2);
hold on
text(170,9.3,'NYSE Open','Color','blue','FontSize',20)
hold on
plot([268 268], [0 9],'--b','Linewidth',2);
hold on
text(240,9.3,'NYSE Close','Color','blue','FontSize',20)
hold on
plot([hour_index(1) hour_index(end)], [1 1],'-k','Linewidth',1);
hold on
xlim([0 EU_end])
ylabel('Normalized Average Volume Traded' )
xlabel('Trade Time (ET)' )
set(gca,'XTick',x_ticks,'XTickLabel',hour_string);
legend off




